import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_demo/helper/live_helper.dart';
import 'package:flutter_app_demo/net/base_data.dart';
import 'package:flutter_app_demo/router.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

void main() async {
  CachedNetworkImage.logLevel = CacheManagerLogLevel.debug;
  // android 状态栏透明，不带一层灰背景
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent, // 状态栏背景颜色设置为透明
    statusBarBrightness: Brightness.light,
    statusBarIconBrightness: Brightness.dark, // 状态栏图标颜色，根据背景颜色调整
  ));
  ModelConvertUtils.init();
  WidgetsFlutterBinding.ensureInitialized();
  LiveHelper.instance.init();
  // await DataReporter.instance.init();
  if (kDebugMode) {
    runApp(const MyApp());
  } else {
    // FlutterBugly.postCatchedException(() {
    //   runApp(const MyApp());
    //   FlutterBugly.init(androidAppId: '8997604274', iOSAppId: '384ea82ec9', debugMode: false);
    // }, debugUpload: false);
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return RefreshConfiguration(
      headerBuilder: () => const MaterialClassicHeader(),
      footerBuilder: () => const ClassicFooter(
        loadingText: '加载中...',
        noDataText: '没有更多数据了',
        failedText: '加载失败，请上拉重试',
      ),
      enableLoadingWhenFailed: true,
      hideFooterWhenNotFull: false,
      enableBallisticLoad: true,
      child: MaterialApp.router(
        title: '微乐心享俱乐部',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          // This is the theme of your application.
          //
          // Try running your application with "flutter run". You'll see the
          // application has a blue toolbar. Then, without quitting the app, try
          // changing the primarySwatch below to Colors.green and then invoke
          // "hot reload" (press "r" in the console where you ran "flutter run",
          // or simply save your changes to "hot reload" in a Flutter IDE).
          // Notice that the counter didn't reset back to zero; the application
          // is not restarted.
          primarySwatch: Colors.orange,
          pageTransitionsTheme: const PageTransitionsTheme(
            builders: _defaultBuilders,
          ),
        ),
        themeMode: ThemeMode.light,
        routerConfig: router,
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
          RefreshLocalizations.delegate,
        ],
        supportedLocales: const [Locale('zh')],
        builder: (context, child) {
          final mediaQuery = MediaQuery.of(context).copyWith(textScaleFactor: 1.0);
          // 先通过 FToastBuilder 包裹 child
          final toastWrappedChild = FToastBuilder()(context, child!);
          // 再通过 MediaQuery 包裹它
          return MediaQuery(data: mediaQuery, child: toastWrappedChild);
        },
      ),
    );
  }

  static const Map<TargetPlatform, PageTransitionsBuilder> _defaultBuilders = <TargetPlatform, PageTransitionsBuilder>{
    TargetPlatform.android: OpenRightwardsPageTransitionsBuilder(),
    TargetPlatform.ohos: OpenRightwardsPageTransitionsBuilder(),
    TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
    TargetPlatform.macOS: CupertinoPageTransitionsBuilder(),
  };
}
